package BlockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 阻塞队列的 ⽣产者消费者
 */
class MyResource {
    private volatile boolean flag = true; //默认开启，进⾏⽣产+消费
    private AtomicInteger atomicInteger = new AtomicInteger();
    ArrayBlockingQueue<String> blockingQueue = null;

    public MyResource(ArrayBlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    /**
     * ⽣产包⼦
     */
    public void myProd() throws InterruptedException {
        while (flag) {
            //⽣产包⼦
            String data = atomicInteger.incrementAndGet() + "";// ++i
            boolean resultValue = blockingQueue.offer(data, 2L, TimeUnit.SECONDS);

            if (resultValue) {
                System.out.println(Thread.currentThread().getName() + "\t插⼊队列" + data + "。。成功");
            } else {
                System.out.println(Thread.currentThread().getName() + "\t插⼊队列" + data + "。。失败");
            }

            //耗时
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "\t⽼板喊停了， Flag已更新为false， 停⽌⽣产");
    }

    /**
     * 购买包⼦
     */
    public void myCons() throws InterruptedException {
        while (flag) {
            //购买包⼦
            String result = blockingQueue.poll(2L, TimeUnit.SECONDS);
            if (null == result || "".equals(result)) {
                // 没有包⼦
                System.out.println(Thread.currentThread().getName() + "\t超过2秒钟没有购买到包⼦， 退出消费。。。");
                return;
            }
            System.out.println(Thread.currentThread().getName() + "\t购买包⼦成功 == = " + result);
        }
    }

    /**
     * ⽼板喊停
     */
    public void stop() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("5秒钟后， ⽼板叫停...");
        flag = false;
    }
}

public class ProdConsBlockQueueDemo {
    public static void main(String[] args) {

        MyResource myResource = new MyResource(new ArrayBlockingQueue<String>(2));

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t⽣产线程启动。。。");
            try {
                myResource.myProd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "师傅A").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t⽣产线程启动。。。");
            try {
                myResource.myProd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "师傅B").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t⽣产线程启动。。。");
            try {
                myResource.myProd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "师傅C").start();

        new Thread(() -> {

            System.out.println(Thread.currentThread().getName() + "\t消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "====顾客A").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "====顾客B").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "====顾客C").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t消费线程启动。。。");
            try {
                myResource.myCons();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "====顾客D").start();

        myResource.stop();
    }
}